import json
from pathlib import Path

img_path = r'15_offline_raw_data'
json_path = r'box.json'
save_dir = r'line_gt'

with open(str(json_path), 'r') as f:
    result = json.load(f)

img_path_glob = Path(img_path).rglob("*.png")
img_path_dict = {x.name: str(x) for x in img_path_glob}

img_info = result['images']
img_info_new = []

for single in img_info:
    img_path = single["file_name"]
    name = Path(img_path).name
    if name in img_path_dict.keys():
        single["file_name"] = img_path_dict[name]
        img_info_new.append(single)

struct_json_lines = {
        'images': img_info_new,
        'annotations': result['annotations'],
        'categories': result['categories'],
    }

result_file_path = Path(save_dir).joinpath('lines_train_local.json')
json.dump(struct_json_lines, open(str(result_file_path), "w"), indent=4)